//
//  CityViewController.m
//  shise
//
//  Created by Darling❤️ on 14/12/18.
//  Copyright (c) 2014年 MZP. All rights reserved.
//

#import "CityViewController.h"
#import "NetHandler.h"

@interface CityViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray *array;
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)UIAlertView *alertView;

@end

@implementation CityViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.array = [NSMutableArray array];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    
    [self handleData];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    
    
}

// 两个协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *string = @"affff";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
        
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string]autorelease];
        
    }
    cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
//    NSLog(@"aaaaaaaaa%@", cell.textLabel.text);
    return cell;
}

// 自定义处理数据的方法
- (void)handleData
{
    
    NSString *address = [NSString stringWithFormat:@"https://api.shiseapp.com/v2/user/profile/location/?_version=2.5.0&conf_ver=1419417114&gps=38.883064,121.539805&sheng=%@&type=localcity&uuid=b611c1feab43d2ee1585a574f539263f", self.province];
    NSLog(@"传过来的省地址%@", address);
    [NetHandler getDataWithUrl:address completion:^(NSData *data) {
        
        NSError *error = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        NSArray *arr = [dic objectForKey:@"data"];
        for (NSDictionary *tempDic in arr) {

            NSString *value = [tempDic objectForKey:@"selectData"];
            if (value.length > 0) {
                
                [self.array addObject:[tempDic objectForKey:@"selectData"]];
                
            }
        }
        [self.tableView reloadData];
    }];
}

// 点击cell的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *string = [self.array objectAtIndex:indexPath.row];
    // 当点击cell的时候使用通知中心将发现页面的“同城美食改成该cell上的数据的城市”
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeCity" object:string userInfo:nil];
    self.alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"更改成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [self.alertView show];
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
}

- (void)performDismiss:(NSTimer *)timer
{
    [self.alertView dismissWithClickedButtonIndex:0 animated:YES];
    [self.alertView release];
    self.alertView = nil;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
